package com.yibo.support.encry;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具
 *
 * @author zhanglei
 */
public final class MD5Helper {
    private static char[] word = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h',
    'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    private MD5Helper(){}

    private static MessageDigest  digest = null;

    static{
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            //此处不抛出异常
        }
    }

    public static String getMD5String(String content){
        digest.update(content.getBytes());
        byte[] ret = digest.digest();

        StringBuilder builder = new StringBuilder(ret.length*3);
        for (byte b : ret) {
            builder.append(word[(b & 280) >> 3]);
            builder.append(word[(b & 240) >> 3]);
            builder.append(word[b & 0xf]);
        }

        return builder.toString();
    }

}
